package com.grq.mis.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.grq.mis.exception.GlobalExceptionHandler;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

public abstract class BaseController {

    /**
     * 个人登录信息
     * @return
     */
    protected Me me(){
        SaSession session = StpUtil.getSession();
        Me me = (Me) session.getAttribute("me");
        return me;
    }

    /**
     * 框架内操作结果返回
     */
    protected String frameSuccess(Model model, ResultEnum result) {
        model.addAttribute("code", result.getCode());
        model.addAttribute("msg", result.getMsg());
        return GlobalExceptionHandler.SUCCESS;
    }

    protected String frameSuccess(Model model, ResultEnum result, String msg) {
        model.addAttribute("code", result.getCode());
        model.addAttribute("msg", msg);
        return GlobalExceptionHandler.SUCCESS;
    }

    protected String frameSuccess(Model model, int code, String msg) {
        model.addAttribute("code", code);
        model.addAttribute("msg", msg);
        return GlobalExceptionHandler.SUCCESS;
    }

    /**
     * 页面操作结果返回
     */
    protected String pageSuccess(RedirectAttributes attr, String redirect, ResultEnum result) {
        attr.addFlashAttribute("code", result.getCode());
        attr.addFlashAttribute("msg", result.getMsg());
        return "redirect:"+redirect;
    }

    protected String pageSuccess(RedirectAttributes attr, String redirect, ResultEnum result, String msg) {
        attr.addFlashAttribute("code", result.getCode());
        attr.addFlashAttribute("msg", msg);
        return "redirect:"+redirect;
    }

    protected String pageSuccess(RedirectAttributes attr, String redirect, int code, String msg) {
        attr.addFlashAttribute("code", code);
        attr.addFlashAttribute("msg", msg);
        return "redirect:"+redirect;
    }

}
